package com.eqdd.yiqidian.ui.select;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Parcelable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.eqdd.yiqidian.MultiSelectActivityCustom;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.ISelectBean;
import com.eqdd.yiqidian.utils.PinYinUtil;
import com.eqdd.yiqidian.utils.ToastUtil;

import java.lang.reflect.Field;
import java.util.ArrayList;

import io.reactivex.Flowable;

/**
 * Created by lv on 17-8-28.
 */

public class MultiSelectActivity extends BaseActivity {

    private MultiSelectActivityCustom dataBinding;
    private int layoutId;
    private ArrayList<ISelectBean> allDatas;
    private ArrayList<ISelectBean> selectDatas;
    private ArrayList<ISelectBean> disableDatas;
    private String compareField;
    private SlimAdapterEx<ISelectBean> slimAdapterEx;
    private int mode;
    private int selectPos = -1;
    private int minSelectNum;
    private int maxSelectNum;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_multi_select);
        initTopTitleBar(View.VISIBLE, "选择");
        initTopRightText("确定", v -> {
            if (selectDatas.size() < minSelectNum) {
                ToastUtil.showShort("最少要选" + minSelectNum + "个");
            } else {
                Intent intent = new Intent();
                intent.putParcelableArrayListExtra(Config.LIST, selectDatas);
                setResult(Config.SUCCESS, intent);
                finish();
            }
        });
    }

    @Override
    public void initData() {
        layoutId = getIntent().getIntExtra("layoutId", -1);
        compareField = getIntent().getStringExtra("compareField");
        allDatas = getIntent().getParcelableArrayListExtra("allDatas");
        selectDatas = getIntent().getParcelableArrayListExtra("selectDatas");
        disableDatas = getIntent().getParcelableArrayListExtra("disableDatas");
        minSelectNum = getIntent().getIntExtra("minSelectNum", 0);
        maxSelectNum = getIntent().getIntExtra("maxSelectNum", 0);
        mode = getIntent().getIntExtra("mode", 0);
        if (selectDatas != null) {
            Flowable.fromIterable(selectDatas)
                    .subscribe(bean -> {
                        for (int i = 0, lenth = allDatas.size(); i < lenth; i++) {
                            if (getField(bean, compareField).equals(getField(allDatas.get(i), compareField))) {
                                allDatas.remove(i);
                                bean.setChecked(true);
                                allDatas.add(i, bean);
                                if (mode == 0) {
                                    selectPos = i;
                                }
                                break;
                            }
                        }
                    });
        } else {
            selectDatas = new ArrayList<>();
        }
        if (disableDatas != null) {
            Flowable.fromIterable(disableDatas)
                    .subscribe(bean -> {
                        for (int i = 0, lenth = allDatas.size(); i < lenth; i++) {
                            if (getField(bean, compareField).equals(getField(allDatas.get(i), compareField))) {
                                allDatas.get(i).setEnable(false);
                                break;
                            }
                        }
                    });
        }
        dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        slimAdapterEx = SlimAdapterEx.create().register(layoutId, new SlimInjector<ISelectBean>() {
            @Override
            public void onInject(ISelectBean data, IViewInjector injector) {
                injector.checked(R.id.checkbox, data.isChecked())
                        .enable(R.id.checkbox, data.isEnable());
                MultiSelectBuilder.getOnInject().inject(data, injector);
            }
        }).attachTo(dataBinding.recyclerView).updateData(allDatas);
        ItemClickSupport.addTo(dataBinding.recyclerView)
                .setOnItemClickListener((recyclerView, position, v) -> {
                    if (slimAdapterEx.getDataItem(position).isEnable()) {
                        slimAdapterEx.getDataItem(position).setChecked(!slimAdapterEx.getDataItem(position).isChecked());
                        if (mode == 1) {
                            if (slimAdapterEx.getDataItem(position).isChecked()) {
                                if (maxSelectNum > 0 && slimAdapterEx.getData().size() >= maxSelectNum) {
                                    slimAdapterEx.getDataItem(position).setChecked(!slimAdapterEx.getDataItem(position).isChecked());
                                    ToastUtil.showShort("最多可选" + maxSelectNum + "个");
                                    return;
                                }
                                selectDatas.add(slimAdapterEx.getDataItem(position));
                            } else {
                                selectDatas.remove(slimAdapterEx.getDataItem(position));
                            }
                            slimAdapterEx.notifyItemChanged(position);
                        } else if (mode == 0) {//单选
                            if (selectPos != -1) {
                                slimAdapterEx.getDataItem(selectPos).setChecked(false);
                                selectDatas.remove(slimAdapterEx.getDataItem(selectPos));
                            }
                            if (position == selectPos) {
                                selectPos = -1;
                                slimAdapterEx.notifyItemChanged(position);
                            } else {
                                selectDatas.add(slimAdapterEx.getDataItem(position));
                                slimAdapterEx.notifyItemChanged(position);
                                slimAdapterEx.notifyItemChanged(selectPos);
                                selectPos = position;
                            }
                        }
                    }
                });

    }

    @Override
    public void setView() {

    }

    private static Object getField(Object o, String field) throws Exception {
        Field declaredField = o.getClass().getDeclaredField(field);
        declaredField.setAccessible(true);
        return declaredField.get(o);
    }
}
